package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.Post;
import com.example.sys.mapper.PostMapper;
import com.example.sys.service.PostService;
import com.example.sys.vo.PostVo;
import jdk.nashorn.internal.ir.ReturnNode;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/post")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("/getPostList")
    public ResponseBean<UnicomResponseEnums> getPostList(PostVo postVo){
        System.out.println(postVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(postVo, false, true);
        if (stringObjectMap.isEmpty()){
            List<Post> posts=postService.list();
            return new ResponseBean(true, posts, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, postService.selectPage(postVo), UnicomResponseEnums.SUCCESS_OPTION);
    }

    @DeleteMapping("/removeByPostId")
    @RequiresPermissions({"system:post:remove"})
    public ResponseBean<UnicomResponseEnums> remoeByPostId(@RequestBody Long postId){
        if (postService.removeById(postId)){
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteByPostId")
    @RequiresPermissions({"system:post:remove"})
    public ResponseBean<UnicomResponseEnums> deleteByPostId(@RequestBody PostVo postVo){
        if (!postVo.getPostId().isEmpty()) {
            postService.removeById(postVo.getPostId());
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/addPost")
    @RequiresPermissions({"system:post:add"})
    public ResponseBean<UnicomResponseEnums> addPost(@RequestBody @Valid Post post){
        if (!post.toString().isEmpty()) {
            postService.save(post);
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true,UnicomResponseEnums.OPERATION_FAILED);

    }

    @PostMapping("/updatePostByRoleId")
    @RequiresPermissions({"system:post:edit"})
    public ResponseBean<UnicomResponseEnums> updatePostByRoleId(@RequestBody @Valid Post post){
        if (!post.toString().isEmpty()) {
            postService.updateById(post);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true,UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updatePostByPostId")
    @RequiresPermissions({"system:post:edit"})
    public ResponseBean<UnicomResponseEnums> updatePostByPostId(@RequestBody @Valid Post post){
        if (!post.toString().isEmpty()) {
            postService.updateById(post);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true,UnicomResponseEnums.OPERATION_FAILED);
    }

}
